<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Tag;
use App\Http\Requests\TagCreateRequest;
class TagController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
		$tags = Tag::all();
		return view('admin.tag.index')->withTags($tags);
    }
	
	
	protected $fields = [
		'tag' => '',
		'title' => '',
		'subtitle' => '',
		'meta_description' => '',
		'page_image' => '',
		'layout' => 'blog.layouts.index',
		'reverse_direction' => 0,
	];
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
		$data = [];
		foreach ($this->fields as $field => $default) {
			$data[$field] = old($field, $default);
		}
	
		return view('admin.tag.create', $data);
		
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  TagCreateRequest
     * @return \Illuminate\Http\Response
     */
    public function store(TagCreateRequest $request)
    {
        //
		$tag = new Tag();
		foreach (array_keys($this->fields) as $field) {
			$tag->$field = $request->get($field);
		}
		$tag->save();
	
		return redirect('/admin/tag')
			->with('success', '标签「' . $tag->tag . '」创建成功.');
		
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
		
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
		$tag = Tag::findOrFail($id);
		$data = ['id' => $id];
		foreach (array_keys($this->fields) as $field) {
			$data[$field] = old($field, $tag->$field);
		}
	
		return view('admin.tag.edit', $data);
		
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(\App\Http\Requests\TagUpdateRequest $request, $id)
    {
        //
		$tag = Tag::findOrFail($id);
	
		foreach (array_keys(array_except($this->fields, ['tag'])) as $field) {
			$tag->$field = $request->get($field);
		}
		$tag->save();
	
		return redirect("/admin/tag/$id/edit")
			->with('success', '修改已保存.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
		$tag = Tag::findOrFail($id);
		$tag->delete();
	
		return redirect('/admin/tag')
			->with('success', '标签「' . $tag->tag . '」已经被删除.');
    }
}
